﻿namespace Home.Wcf
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Dispatcher;
    using System.Text;
    using System.Threading.Tasks;
    using Home.ErrorHandling;

    public class WcfErrorHandler : IErrorHandler
    {
        public string Namesapce { get; set; }

        public virtual bool HandleError(Exception error)
        {
            HandleErrorHelper.Handle(error);
            return false;
        }

        public virtual void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            var faultException = error as FaultException;
            if (faultException == null)
            {
                faultException = new FaultException("A unpredictable error occurred on the server side.", new FaultCode("Unknown", this.Namesapce));
            }

            var messageFault = faultException.CreateMessageFault();
            fault = Message.CreateMessage(version, messageFault, faultException.Action);
        }
    }
}
